package com.authine.cloudpivot.web.api.secondopen.controller;

import com.authine.cloudpivot.web.api.secondopen.domain.DingDingUser;
import com.authine.cloudpivot.web.api.secondopen.service.SyncService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 同步控制器
 *
 */
@Api(value = "二开接口", tags = "同步控制器")
@RestController
@RequestMapping("/api/sync")
public class SyncController {
    private static final Logger log = LoggerFactory.getLogger(SyncController.class);

    @Autowired
    private SyncService syncService;

    /**
     * 定时：每30分钟同步一次全年预算额度信息
     *
     * @return
     */
    @Scheduled(cron = "0 0/30 * * * ?")
    public void syncAnnualBudget(){
        log.info("-----------同步开始------------");
        syncService.syncAnnualBudgetInService();
        log.info("-----------同步结束------------");
    }

    /**
     * 手动同步方法，同步全年预算额度信息
     *
     */
    @GetMapping("/syncAnnualBudgetInget")
    public void syncAnnualBudgetInget(){
        log.info("-----------同步开始------------");
        syncService.syncAnnualBudgetInService();
        log.info("-----------同步结束------------");
    }

    /**
     * 同步成本中心数据
     *
     * @param IcAcctFrom
     * @param IcAcctTo
     * @param IcSubFrom
     * @param IcSubTo
     * @return
     */
    @RequestMapping(value = "/syncMdmCostCenter", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(value = "同步成本中心数据", notes = "同步成本中心数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "IcAcctFrom", value = "总账账户开始", dataType = "String", required = false),
            @ApiImplicitParam(name = "IcAcctTo", value = "总账账户结束", dataType = "String", required = false),
            @ApiImplicitParam(name = "IcSubFrom", value = "分账户开始", dataType = "String", required = false),
            @ApiImplicitParam(name = "IcSubTo", value = "分账户结束", dataType = "String", required = false)
    })
    public void syncMdmCostCenter(@RequestParam(value = "IcAcctFrom", required = false, defaultValue = "") String IcAcctFrom,
                                  @RequestParam(value = "IcAcctTo", required = false, defaultValue = "") String IcAcctTo,
                                  @RequestParam(value = "IcSubFrom", required = false, defaultValue = "") String IcSubFrom,
                                  @RequestParam(value = "IcSubTo", required = false, defaultValue = "") String IcSubTo) {
        Map<String, String> params = new HashMap<>();
//        params.put("IcAcctFrom", IcAcctFrom);
//        params.put("IcAcctTo", IcAcctTo);
//        params.put("IcSubFrom", IcSubFrom);
//        params.put("IcSubTo", IcSubTo);
        syncService.syncMdmCostCenter(params);
    }

    @RequestMapping(value = "/syncCurrency", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(value = "同步币种")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "qadCuCurr", value = "货币", dataType = "String", required = false),
            @ApiImplicitParam(name = "qadCuDesc", value = "描述", dataType = "String", required = false)
    })
    public void syncCurrency(@RequestParam(value = "qadCuCurr", required = false, defaultValue = "")String qadCuCurr,
                             @RequestParam(value = "qadCuDesc", required = false, defaultValue = "")String qadCuDesc){
        Map<String, String> params = new HashMap<>();
        if (!StringUtils.isBlank(qadCuCurr)) {
            params.put("qad_cu_curr", qadCuCurr);
        }
        if (!StringUtils.isBlank(qadCuDesc)) {
            params.put("qad_cu_desc", qadCuDesc);
        }
        syncService.syncCurrency(params);
    }

    @RequestMapping(value = "/syncAccount", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(value = "同步账户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "qadAcCode", value = "账户", dataType = "String", required = false),
            @ApiImplicitParam(name = "qadAcType", value = "Type", dataType = "String", required = false),
            @ApiImplicitParam(name = "qadAcCode", value = "描述", dataType = "String", required = false),
            @ApiImplicitParam(name = "qadAcDesc", value = "货币", dataType = "String", required = false),
            @ApiImplicitParam(name = "qadAcDomain", value = "域", dataType = "String", required = false)
    })
    public int syncAccount(@RequestParam(value = "qadAcCode", required = false, defaultValue = "")String qadAcCode,
                           @RequestParam(value = "qadAcType", required = false, defaultValue = "")String qadAcType,
                           @RequestParam(value = "qadAcDesc", required = false, defaultValue = "")String qadAcDesc,
                           @RequestParam(value = "qadAcCurr", required = false, defaultValue = "")String qadAcCurr,
                           @RequestParam(value = "qadAcDomain", required = false, defaultValue = "")String qadAcDomain){
        Map<String, String> params = new HashMap<>();
        if (StringUtils.isNotBlank(qadAcCode)) {
            params.put("qad_ac_code", qadAcCode);
        }
        if (StringUtils.isNotBlank(qadAcType)) {
            params.put("qad_ac_type", qadAcType);
        }
        if (StringUtils.isNotBlank(qadAcDesc)) {
            params.put("qad_ac_desc", qadAcDesc);
        }
        if (StringUtils.isNotBlank(qadAcCurr)) {
            params.put("qad_ac_curr", qadAcCurr);
        }
        if (StringUtils.isNotBlank(qadAcDomain)) {
            params.put("qad_ac_domain", qadAcDomain);
        }
        int syncDataNum = syncService.syncAccount(params);
        return syncDataNum;
    }

    @RequestMapping(value = "/syncSubAccount", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(value = "同步账户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "qadSbSub", value = "账户", dataType = "String", required = false),
            @ApiImplicitParam(name = "qadSbDesc", value = "描述", dataType = "String", required = false),
            @ApiImplicitParam(name = "qadSbDomain", value = "域", dataType = "String", required = false)
    })
    public int syncSubAccount(@RequestParam(value = "qadSbSub", required = false, defaultValue = "")String qadSbSub,
                           @RequestParam(value = "qadSbDesc", required = false, defaultValue = "")String qadSbDesc,
                           @RequestParam(value = "qadSbDomain", required = false, defaultValue = "")String qadSbDomain){
        Map<String, String> params = new HashMap<>();
        if (StringUtils.isNotBlank(qadSbSub)) {
            params.put("qad_sb_sub", qadSbSub);
        }

        if (StringUtils.isNotBlank(qadSbDesc)) {
            params.put("qad_sb_desc", qadSbDesc);
        }

        if (StringUtils.isNotBlank(qadSbDomain)) {
            params.put("qad_sb_domain", qadSbDomain);
        }
        int syncDataNum = syncService.syncSubAccount(params);
        return syncDataNum;
    }

    @Scheduled(cron = "0 3 3 * * ?")
    public void syncAccountAuto() {
        log.info("-----------同步开始------------");
        syncService.syncAccount(new HashMap<>());
        log.info("-----------同步结束------------");
    }

    @Scheduled(cron = "0 13 3 * * ?")
    public void syncSubAccountAuto() {
        log.info("-----------同步开始------------");
        syncService.syncSubAccount(new HashMap<>());
        log.info("-----------同步结束------------");
    }

    @Scheduled(cron = "0 3 4 * * ?")
    public void syncMdmCostCenterAuto() {
        log.info("-----------同步开始------------");
        syncService.syncMdmCostCenter(new HashMap<>());
        log.info("-----------同步结束------------");
    }

    @Scheduled(cron = "0 23 3 * * ?")
    public void syncCurrencyAuto() {
        log.info("-----------同步开始------------");
        syncService.syncCurrency(new HashMap<>());
        log.info("-----------同步结束------------");
    }

    /**
     * 同步成本中心----全量
     */
    @RequestMapping(value = "/syncCostCentertoBPM")
    public void syncCCToBPM(@RequestParam(value = "qad_cc_ctr",required = false,defaultValue = "") String qad_cc_ctr,
                            @RequestParam(value = "qad_cc_desc",required = false,defaultValue = "") String qad_cc_desc,
                            @RequestParam(value = "qad_cc_domain",required = false,defaultValue = "") String qad_cc_domain){
        Map<String,String> params=new HashMap<>();
        if (StringUtils.isNotBlank(qad_cc_ctr)){
            params.put("qad_cc_ctr",qad_cc_ctr);
        }
        if (StringUtils.isNotBlank(qad_cc_desc)){
            params.put("qad_cc_desc",qad_cc_desc);
        }
        if (StringUtils.isNotBlank(qad_cc_domain)){
            params.put("qad_cc_domain",qad_cc_domain);
        }
        syncService.syncCCToBPM(params);
    }

    /**
     * 同步成本中心----全量
     */
    @Scheduled(cron = "0 10 4 * * ?")
    public void syncCCToBPMData(){
        Map<String,String> params=new HashMap<>();
        syncService.syncCCToBPM(params);
    }


    /**
     * 同步会计单位信息
     *
     */
//    @Scheduled(cron = "0 20 4 * * ?")
    @RequestMapping(value = "/syncKjdwxx")
    public void syncKjdwxx() {
        log.info("-----------同步会计单位信息开始------------");
        syncService.syncKjdwxx();
        log.info("-----------同步会计单位结束------------");
    }


    /**
     * 同步会计单位信息
     *
     */
//    @Scheduled(cron = "0 20 4 * * ?")
//    @Scheduled(cron = "0 20 4 * * ?")
    public void syncKjdwxxAuto() {
        log.info("-----------同步会计单位信息开始------------");
        syncService.syncKjdwxx();
        log.info("-----------同步会计单位结束------------");
    }



    /**
     * 同步部门-全量
     */
    //@Scheduled(cron = "0 3 3 * * ?")
    public void syncDep() {
        log.info("============开始同步部门=============");
        syncService.syncDep();
        log.info("============同步部门结束============");
    }

    /**
     * 同步部门-增量
     * 增量只做新增
     */
    //@Scheduled(cron = "0 0 3 * * ?")
    public void incrementdep(){
        log.info("=============开始同步部门==============");
        syncService.incrementdep();
        log.info("=============同步部门结束==============");
    }

    /**
     * 同步nc人员信息至钉钉-全量
     */
    //@Scheduled(cron = "0 5 3 * * ?")
    //@PostMapping("/syncdd")
    public void syncDD() {
        log.info("=============开始同步人员===========");
        syncService.syncDD();
        log.info("=============同步人员结束===========");
    }

    /**
     * 同步nc人员信息至钉钉-增量
     * 增量只做新增
     */
    //@Scheduled(cron = "0 1 3 * * ?")
    public void incrementuser(){
        log.info("=============开始同步人员===========");
        syncService.incrementuser();
        log.info("=============同步人员结束===========");
    }

    /**
     * 获取人员信息---同城
     * @return
     */
    @RequestMapping("/getuserinfo")
    public List<DingDingUser> getUserInfo(){
        return syncService.getUserInfo();
    }
}
